<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>微服务实例</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"/>
    <link rel="stylesheet" th:href="@{/admin/css/pearForm.css}"/>
    <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">

    <style type="text/css">

        body {
            /*margin: 10px;*/
            background-color: whitesmoke;
            font-family: 'Roboto', sans-serif;
            font-size: 14px;
        }
    </style>
</head>
<body>
<div th:include="nav::nav"></div>
<div class="layui-container">

    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn" lay-event="importServiceApiTemplateAndCreateProject">导入服务API并创建项目</button>
            <button class="layui-btn" lay-event="importServiceApiTemplateToExistProject">导入服务API到已创建项目</button>
        </div>
    </script>

    <table class="layui-hide" style="font-family: Roboto,serif;font-size: 15px" id="test" lay-filter="test"></table>
</div>

<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/admin/js/jquery.js}"></script>
<script th:src="@{/admin/js/util.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test'
            , url: '/restdoc/serviceClient/list'
            , toolbar: "#toolbar"
            , size: 'lg'
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'}
                    ,
                    {
                        field: 'service', width: 250, title: '服务名称', sort: true
                    }
                    ,
                    {
                        field: 'remoteAddress', width: 250, title: '服务远程地址', sort: true
                    }
                    ,
                    {
                        field: 'osname', width: 250, title: '运行平台'
                    }
                    ,
                    {
                        field: 'hostname', width: 250, title: '计算机名称'
                    }
                ]
            ]

        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);

            var data = checkStatus.data;

            switch (obj.event) {
                case 'importServiceApiTemplateAndCreateProject':

                    console.info(data)

                    var json = {
                        name: data[0]['service'],
                        clientId: data[0]['remoteAddress']
                    };

                    console.info(json)

                    $.ajax({
                        method: 'POST',
                        dataType: 'JSON',
                        contentType: 'application/json;charset=utf-8',
                        url: '/restdoc/serviceClient/apiEmptyTemplate/sync',
                        data: JSON.stringify(json),
                        async: false,
                        success: function (data) {
                            if (data['code'] === "200") {
                                if (data['data'] != null) {
                                    layer.msg('操作成功');
                                    var projectId = data['data'];

                                    layer.open({
                                        content: "<a class='layui-btn' target='_self' href='/restdoc/" + projectId + "/document/nav/view'>去查看</a>"
                                        , btn: ['确定',]
                                        , yes: function (index, layero) {
                                            layer.close(index)
                                        }
                                    });

                                } else {
                                    layer.msg("操作失败")
                                }
                            } else {
                                layer.msg(data['message'])
                            }
                        },
                        error: function () {
                            layer.msg("网络异常")
                        }
                    });


                    break;


                case 'importServiceApiTemplateToExistProject':

                    break;
            }
        });

    });

    $("#serviceClientNav").addClass("layui-this")

</script>
</body>
</html>